package com.itheima.controller;

import com.itheima.common.Result;
import com.itheima.pojo.Bank;
import com.itheima.service.BankService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/bank")
public class BankController {

    @Autowired
    private BankService bankService;

    /**
     * 根据城市id查询所有银行网点
     *
     * @param areaid 城市id，-1表示没有选择城市
     * @return
     */
    @GetMapping("/list/{areaid}")
    public Result findAll(@PathVariable Integer areaid) {
        List<Bank> bankList = bankService.findAll(areaid);
        return Result.ok("查询银行网点成功", bankList);
    }


    /**
     * 新增银行网点信息
     *
     * @param bank
     */
    @PostMapping
    public Result add(@RequestBody Bank bank) {
        log.info("新增银行网点，参数为：{}", bank);
        bankService.add(bank);
        return Result.ok("新增银行网点成功！！");
    }

    /**
     * 修改银行网点时候的数据回显
     *
     * @param id
     * @return
     */
    @GetMapping("/getById/{id}")
    public Result findById(@PathVariable Integer id) {
        log.info("修改银行网点，参数为：{}", id);
        Bank bank = bankService.findById(id);
        return Result.ok("修改银行网点", bank);
    }

    /**
     * 修改网点
     * @param bank
     * @return
     */
    @PutMapping
    public  Result update(@RequestBody Bank bank){
        bankService.update(bank);
        return Result.ok("修改银行网点成功！");
    }

    /**
     * 银行网点的删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        bankService.deleteById(id);
        return Result.ok("删除银行网点成功！");
    }
}

